#include <stdio.h>
#include <stdlib.h>

/*
	1.使用宏定义取代定义多个结构体变量
	2.无符号变量的溢出和按位反转的实例
*/
struct test {
	int a;
	char b;
	unsigned int width_test;
	unsigned int height_test;
	int c[10];
};

#define TEST_INIT(width, height) { \
	.a = 1, \
	.b = 'A', \
	.width_test = width, \
	.height_test = height, \
	{width, height, 2, 3},\
}

static struct test test_arry[] = {
	[0] = TEST_INIT(1024, 768),
	[1] = TEST_INIT(800, 600),
	[2] = TEST_INIT(1280, 720),
	
};

/* 按位反转
0000 1001 0000 1001
1001 0000 1001 0000
 */
unsigned int 
reverse_bits(unsigned int value)
{
	unsigned int answer;
	unsigned int i;

	answer = 0;

	/*把一个unsigned int 数字1一直左移，直到它变成全0的时候，也就得到了该机器内unsigned int的长度*/
	for (i = 1; i != 0; i <<= 1) {
		answer <<= 1;
		if (value & 1) { answer |= 1; }
		value >>= 1;
	}

	return answer;
}

int main(void)
{
	int i;
	unsigned int a = 1;
	unsigned int uiTemp = 1;
	for (i = 0; i < sizeof(test_arry) / sizeof(test_arry[0]); i++) {
		printf("test_arry[%d].a = %d\n", i, test_arry[i].a);	
		printf("test_arry[%d].b = %c\n", i, test_arry[i].b);
		printf("test_arry[%d].width = %u\n", i, test_arry[i].width_test);
		printf("test_arry[%d].height = %u\n", i, test_arry[i].height_test);
		printf("test_arry[%d].c[%d] = %d\n", i, i, test_arry[i].c[i]);
	}

	printf("a = %x\n", a <<= 1);

	/* 无符号反转 加后者&&条件可正常打印 */
	//for(uiTemp = 1; uiTemp <= 0x8000000 && uiTemp != 0; uiTemp <<= 1) {
	i = 0;
	for(uiTemp = 1; uiTemp != 0; uiTemp <<= 1) {/* 遍历每一bit (不依赖机器的unsigned int的具体字节数) */
		printf("uiTemp = 0x%x\n", uiTemp);
		printf("i = %d\n", i++);
		//printf("uiTemp = %u\n", uiTemp);
		//sleep(1);
	}
	return 0;
}
